/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Occurrenceelement;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Sex;
import entityBeans.Reproductivecondition;
import entityBeans.Lifestage;
import entityBeans.Establishmentmean;
import entityBeans.Disposition;
import entityBeans.Behavior;
import entityBeans.Individual;
import java.util.ArrayList;
import java.util.List;
import entityBeans.Recordedby;
import entityBeans.Associatedsequence;
import entityBeans.Preparation;
import entityBeans.Specimen;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class OccurrenceelementJpaController {

    public OccurrenceelementJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Occurrenceelement occurrenceelement) {
        if (occurrenceelement.getIndividualList() == null) {
            occurrenceelement.setIndividualList(new ArrayList<Individual>());
        }
        if (occurrenceelement.getRecordedbyList() == null) {
            occurrenceelement.setRecordedbyList(new ArrayList<Recordedby>());
        }
        if (occurrenceelement.getAssociatedsequenceList() == null) {
            occurrenceelement.setAssociatedsequenceList(new ArrayList<Associatedsequence>());
        }
        if (occurrenceelement.getPreparationList() == null) {
            occurrenceelement.setPreparationList(new ArrayList<Preparation>());
        }
        if (occurrenceelement.getSpecimenList() == null) {
            occurrenceelement.setSpecimenList(new ArrayList<Specimen>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Sex sex = occurrenceelement.getSex();
            if (sex != null) {
                sex = em.getReference(sex.getClass(), sex.getIdsex());
                occurrenceelement.setSex(sex);
            }
            Reproductivecondition reproductivecondition = occurrenceelement.getReproductivecondition();
            if (reproductivecondition != null) {
                reproductivecondition = em.getReference(reproductivecondition.getClass(), reproductivecondition.getIdreproductivecondition());
                occurrenceelement.setReproductivecondition(reproductivecondition);
            }
            Lifestage lifestage = occurrenceelement.getLifestage();
            if (lifestage != null) {
                lifestage = em.getReference(lifestage.getClass(), lifestage.getIdlifestage());
                occurrenceelement.setLifestage(lifestage);
            }
            Establishmentmean establishmentmean = occurrenceelement.getEstablishmentmean();
            if (establishmentmean != null) {
                establishmentmean = em.getReference(establishmentmean.getClass(), establishmentmean.getIdestablishmentmean());
                occurrenceelement.setEstablishmentmean(establishmentmean);
            }
            Disposition disposition = occurrenceelement.getDisposition();
            if (disposition != null) {
                disposition = em.getReference(disposition.getClass(), disposition.getIddisposition());
                occurrenceelement.setDisposition(disposition);
            }
            Behavior behavior = occurrenceelement.getBehavior();
            if (behavior != null) {
                behavior = em.getReference(behavior.getClass(), behavior.getIdbehavior());
                occurrenceelement.setBehavior(behavior);
            }
            List<Individual> attachedIndividualList = new ArrayList<Individual>();
            for (Individual individualListIndividualToAttach : occurrenceelement.getIndividualList()) {
                individualListIndividualToAttach = em.getReference(individualListIndividualToAttach.getClass(), individualListIndividualToAttach.getIdindividual());
                attachedIndividualList.add(individualListIndividualToAttach);
            }
            occurrenceelement.setIndividualList(attachedIndividualList);
            List<Recordedby> attachedRecordedbyList = new ArrayList<Recordedby>();
            for (Recordedby recordedbyListRecordedbyToAttach : occurrenceelement.getRecordedbyList()) {
                recordedbyListRecordedbyToAttach = em.getReference(recordedbyListRecordedbyToAttach.getClass(), recordedbyListRecordedbyToAttach.getIdrecordedby());
                attachedRecordedbyList.add(recordedbyListRecordedbyToAttach);
            }
            occurrenceelement.setRecordedbyList(attachedRecordedbyList);
            List<Associatedsequence> attachedAssociatedsequenceList = new ArrayList<Associatedsequence>();
            for (Associatedsequence associatedsequenceListAssociatedsequenceToAttach : occurrenceelement.getAssociatedsequenceList()) {
                associatedsequenceListAssociatedsequenceToAttach = em.getReference(associatedsequenceListAssociatedsequenceToAttach.getClass(), associatedsequenceListAssociatedsequenceToAttach.getIdassociatedsequence());
                attachedAssociatedsequenceList.add(associatedsequenceListAssociatedsequenceToAttach);
            }
            occurrenceelement.setAssociatedsequenceList(attachedAssociatedsequenceList);
            List<Preparation> attachedPreparationList = new ArrayList<Preparation>();
            for (Preparation preparationListPreparationToAttach : occurrenceelement.getPreparationList()) {
                preparationListPreparationToAttach = em.getReference(preparationListPreparationToAttach.getClass(), preparationListPreparationToAttach.getIdpreparation());
                attachedPreparationList.add(preparationListPreparationToAttach);
            }
            occurrenceelement.setPreparationList(attachedPreparationList);
            List<Specimen> attachedSpecimenList = new ArrayList<Specimen>();
            for (Specimen specimenListSpecimenToAttach : occurrenceelement.getSpecimenList()) {
                specimenListSpecimenToAttach = em.getReference(specimenListSpecimenToAttach.getClass(), specimenListSpecimenToAttach.getIdspecimen());
                attachedSpecimenList.add(specimenListSpecimenToAttach);
            }
            occurrenceelement.setSpecimenList(attachedSpecimenList);
            em.persist(occurrenceelement);
            if (sex != null) {
                sex.getOccurrenceelementList().add(occurrenceelement);
                sex = em.merge(sex);
            }
            if (reproductivecondition != null) {
                reproductivecondition.getOccurrenceelementList().add(occurrenceelement);
                reproductivecondition = em.merge(reproductivecondition);
            }
            if (lifestage != null) {
                lifestage.getOccurrenceelementList().add(occurrenceelement);
                lifestage = em.merge(lifestage);
            }
            if (establishmentmean != null) {
                establishmentmean.getOccurrenceelementList().add(occurrenceelement);
                establishmentmean = em.merge(establishmentmean);
            }
            if (disposition != null) {
                disposition.getOccurrenceelementList().add(occurrenceelement);
                disposition = em.merge(disposition);
            }
            if (behavior != null) {
                behavior.getOccurrenceelementList().add(occurrenceelement);
                behavior = em.merge(behavior);
            }
            for (Individual individualListIndividual : occurrenceelement.getIndividualList()) {
                individualListIndividual.getOccurrenceelementList().add(occurrenceelement);
                individualListIndividual = em.merge(individualListIndividual);
            }
            for (Recordedby recordedbyListRecordedby : occurrenceelement.getRecordedbyList()) {
                recordedbyListRecordedby.getOccurrenceelementList().add(occurrenceelement);
                recordedbyListRecordedby = em.merge(recordedbyListRecordedby);
            }
            for (Associatedsequence associatedsequenceListAssociatedsequence : occurrenceelement.getAssociatedsequenceList()) {
                associatedsequenceListAssociatedsequence.getOccurrenceelementList().add(occurrenceelement);
                associatedsequenceListAssociatedsequence = em.merge(associatedsequenceListAssociatedsequence);
            }
            for (Preparation preparationListPreparation : occurrenceelement.getPreparationList()) {
                preparationListPreparation.getOccurrenceelementList().add(occurrenceelement);
                preparationListPreparation = em.merge(preparationListPreparation);
            }
            for (Specimen specimenListSpecimen : occurrenceelement.getSpecimenList()) {
                Occurrenceelement oldOccurrenceelementOfSpecimenListSpecimen = specimenListSpecimen.getOccurrenceelement();
                specimenListSpecimen.setOccurrenceelement(occurrenceelement);
                specimenListSpecimen = em.merge(specimenListSpecimen);
                if (oldOccurrenceelementOfSpecimenListSpecimen != null) {
                    oldOccurrenceelementOfSpecimenListSpecimen.getSpecimenList().remove(specimenListSpecimen);
                    oldOccurrenceelementOfSpecimenListSpecimen = em.merge(oldOccurrenceelementOfSpecimenListSpecimen);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Occurrenceelement occurrenceelement) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Occurrenceelement persistentOccurrenceelement = em.find(Occurrenceelement.class, occurrenceelement.getIdoccurrenceelement());
            Sex sexOld = persistentOccurrenceelement.getSex();
            Sex sexNew = occurrenceelement.getSex();
            Reproductivecondition reproductiveconditionOld = persistentOccurrenceelement.getReproductivecondition();
            Reproductivecondition reproductiveconditionNew = occurrenceelement.getReproductivecondition();
            Lifestage lifestageOld = persistentOccurrenceelement.getLifestage();
            Lifestage lifestageNew = occurrenceelement.getLifestage();
            Establishmentmean establishmentmeanOld = persistentOccurrenceelement.getEstablishmentmean();
            Establishmentmean establishmentmeanNew = occurrenceelement.getEstablishmentmean();
            Disposition dispositionOld = persistentOccurrenceelement.getDisposition();
            Disposition dispositionNew = occurrenceelement.getDisposition();
            Behavior behaviorOld = persistentOccurrenceelement.getBehavior();
            Behavior behaviorNew = occurrenceelement.getBehavior();
            List<Individual> individualListOld = persistentOccurrenceelement.getIndividualList();
            List<Individual> individualListNew = occurrenceelement.getIndividualList();
            List<Recordedby> recordedbyListOld = persistentOccurrenceelement.getRecordedbyList();
            List<Recordedby> recordedbyListNew = occurrenceelement.getRecordedbyList();
            List<Associatedsequence> associatedsequenceListOld = persistentOccurrenceelement.getAssociatedsequenceList();
            List<Associatedsequence> associatedsequenceListNew = occurrenceelement.getAssociatedsequenceList();
            List<Preparation> preparationListOld = persistentOccurrenceelement.getPreparationList();
            List<Preparation> preparationListNew = occurrenceelement.getPreparationList();
            List<Specimen> specimenListOld = new ArrayList<Specimen>(); //persistentOccurrenceelement.getSpecimenList();
            List<Specimen> specimenListNew = new ArrayList<Specimen>(); //occurrenceelement.getSpecimenList();
            if (sexNew != null) {
                sexNew = em.getReference(sexNew.getClass(), sexNew.getIdsex());
                occurrenceelement.setSex(sexNew);
            }
            if (reproductiveconditionNew != null) {
                reproductiveconditionNew = em.getReference(reproductiveconditionNew.getClass(), reproductiveconditionNew.getIdreproductivecondition());
                occurrenceelement.setReproductivecondition(reproductiveconditionNew);
            }
            if (lifestageNew != null) {
                lifestageNew = em.getReference(lifestageNew.getClass(), lifestageNew.getIdlifestage());
                occurrenceelement.setLifestage(lifestageNew);
            }
            if (establishmentmeanNew != null) {
                establishmentmeanNew = em.getReference(establishmentmeanNew.getClass(), establishmentmeanNew.getIdestablishmentmean());
                occurrenceelement.setEstablishmentmean(establishmentmeanNew);
            }
            if (dispositionNew != null) {
                dispositionNew = em.getReference(dispositionNew.getClass(), dispositionNew.getIddisposition());
                occurrenceelement.setDisposition(dispositionNew);
            }
            if (behaviorNew != null) {
                behaviorNew = em.getReference(behaviorNew.getClass(), behaviorNew.getIdbehavior());
                occurrenceelement.setBehavior(behaviorNew);
            }
            List<Individual> attachedIndividualListNew = new ArrayList<Individual>();
            for (Individual individualListNewIndividualToAttach : individualListNew) {
                individualListNewIndividualToAttach = em.getReference(individualListNewIndividualToAttach.getClass(), individualListNewIndividualToAttach.getIdindividual());
                attachedIndividualListNew.add(individualListNewIndividualToAttach);
            }
            individualListNew = attachedIndividualListNew;
            occurrenceelement.setIndividualList(individualListNew);
            List<Recordedby> attachedRecordedbyListNew = new ArrayList<Recordedby>();
            for (Recordedby recordedbyListNewRecordedbyToAttach : recordedbyListNew) {
                recordedbyListNewRecordedbyToAttach = em.getReference(recordedbyListNewRecordedbyToAttach.getClass(), recordedbyListNewRecordedbyToAttach.getIdrecordedby());
                attachedRecordedbyListNew.add(recordedbyListNewRecordedbyToAttach);
            }
            recordedbyListNew = attachedRecordedbyListNew;
            occurrenceelement.setRecordedbyList(recordedbyListNew);
            List<Associatedsequence> attachedAssociatedsequenceListNew = new ArrayList<Associatedsequence>();
            for (Associatedsequence associatedsequenceListNewAssociatedsequenceToAttach : associatedsequenceListNew) {
                associatedsequenceListNewAssociatedsequenceToAttach = em.getReference(associatedsequenceListNewAssociatedsequenceToAttach.getClass(), associatedsequenceListNewAssociatedsequenceToAttach.getIdassociatedsequence());
                attachedAssociatedsequenceListNew.add(associatedsequenceListNewAssociatedsequenceToAttach);
            }
            associatedsequenceListNew = attachedAssociatedsequenceListNew;
            occurrenceelement.setAssociatedsequenceList(associatedsequenceListNew);
            List<Preparation> attachedPreparationListNew = new ArrayList<Preparation>();
            for (Preparation preparationListNewPreparationToAttach : preparationListNew) {
                preparationListNewPreparationToAttach = em.getReference(preparationListNewPreparationToAttach.getClass(), preparationListNewPreparationToAttach.getIdpreparation());
                attachedPreparationListNew.add(preparationListNewPreparationToAttach);
            }
            preparationListNew = attachedPreparationListNew;
            occurrenceelement.setPreparationList(preparationListNew);
            List<Specimen> attachedSpecimenListNew = new ArrayList<Specimen>();
            for (Specimen specimenListNewSpecimenToAttach : specimenListNew) {
                specimenListNewSpecimenToAttach = em.getReference(specimenListNewSpecimenToAttach.getClass(), specimenListNewSpecimenToAttach.getIdspecimen());
                attachedSpecimenListNew.add(specimenListNewSpecimenToAttach);
            }
            specimenListNew = attachedSpecimenListNew;
            occurrenceelement.setSpecimenList(specimenListNew);
            occurrenceelement = em.merge(occurrenceelement);
            if (sexOld != null && !sexOld.equals(sexNew)) {
                sexOld.getOccurrenceelementList().remove(occurrenceelement);
                sexOld = em.merge(sexOld);
            }
            if (sexNew != null && !sexNew.equals(sexOld)) {
                sexNew.getOccurrenceelementList().add(occurrenceelement);
                sexNew = em.merge(sexNew);
            }
            if (reproductiveconditionOld != null && !reproductiveconditionOld.equals(reproductiveconditionNew)) {
                reproductiveconditionOld.getOccurrenceelementList().remove(occurrenceelement);
                reproductiveconditionOld = em.merge(reproductiveconditionOld);
            }
            if (reproductiveconditionNew != null && !reproductiveconditionNew.equals(reproductiveconditionOld)) {
                reproductiveconditionNew.getOccurrenceelementList().add(occurrenceelement);
                reproductiveconditionNew = em.merge(reproductiveconditionNew);
            }
            if (lifestageOld != null && !lifestageOld.equals(lifestageNew)) {
                lifestageOld.getOccurrenceelementList().remove(occurrenceelement);
                lifestageOld = em.merge(lifestageOld);
            }
            if (lifestageNew != null && !lifestageNew.equals(lifestageOld)) {
                lifestageNew.getOccurrenceelementList().add(occurrenceelement);
                lifestageNew = em.merge(lifestageNew);
            }
            if (establishmentmeanOld != null && !establishmentmeanOld.equals(establishmentmeanNew)) {
                establishmentmeanOld.getOccurrenceelementList().remove(occurrenceelement);
                establishmentmeanOld = em.merge(establishmentmeanOld);
            }
            if (establishmentmeanNew != null && !establishmentmeanNew.equals(establishmentmeanOld)) {
                establishmentmeanNew.getOccurrenceelementList().add(occurrenceelement);
                establishmentmeanNew = em.merge(establishmentmeanNew);
            }
            if (dispositionOld != null && !dispositionOld.equals(dispositionNew)) {
                dispositionOld.getOccurrenceelementList().remove(occurrenceelement);
                dispositionOld = em.merge(dispositionOld);
            }
            if (dispositionNew != null && !dispositionNew.equals(dispositionOld)) {
                dispositionNew.getOccurrenceelementList().add(occurrenceelement);
                dispositionNew = em.merge(dispositionNew);
            }
            if (behaviorOld != null && !behaviorOld.equals(behaviorNew)) {
                behaviorOld.getOccurrenceelementList().remove(occurrenceelement);
                behaviorOld = em.merge(behaviorOld);
            }
            if (behaviorNew != null && !behaviorNew.equals(behaviorOld)) {
                behaviorNew.getOccurrenceelementList().add(occurrenceelement);
                behaviorNew = em.merge(behaviorNew);
            }
            for (Individual individualListOldIndividual : individualListOld) {
                if (!individualListNew.contains(individualListOldIndividual)) {
                    individualListOldIndividual.getOccurrenceelementList().remove(occurrenceelement);
                    individualListOldIndividual = em.merge(individualListOldIndividual);
                }
            }
            for (Individual individualListNewIndividual : individualListNew) {
                if (!individualListOld.contains(individualListNewIndividual)) {
                    individualListNewIndividual.getOccurrenceelementList().add(occurrenceelement);
                    individualListNewIndividual = em.merge(individualListNewIndividual);
                }
            }
            for (Recordedby recordedbyListOldRecordedby : recordedbyListOld) {
                if (!recordedbyListNew.contains(recordedbyListOldRecordedby)) {
                    recordedbyListOldRecordedby.getOccurrenceelementList().remove(occurrenceelement);
                    recordedbyListOldRecordedby = em.merge(recordedbyListOldRecordedby);
                }
            }
            for (Recordedby recordedbyListNewRecordedby : recordedbyListNew) {
                if (!recordedbyListOld.contains(recordedbyListNewRecordedby)) {
                    recordedbyListNewRecordedby.getOccurrenceelementList().add(occurrenceelement);
                    recordedbyListNewRecordedby = em.merge(recordedbyListNewRecordedby);
                }
            }
            for (Associatedsequence associatedsequenceListOldAssociatedsequence : associatedsequenceListOld) {
                if (!associatedsequenceListNew.contains(associatedsequenceListOldAssociatedsequence)) {
                    associatedsequenceListOldAssociatedsequence.getOccurrenceelementList().remove(occurrenceelement);
                    associatedsequenceListOldAssociatedsequence = em.merge(associatedsequenceListOldAssociatedsequence);
                }
            }
            for (Associatedsequence associatedsequenceListNewAssociatedsequence : associatedsequenceListNew) {
                if (!associatedsequenceListOld.contains(associatedsequenceListNewAssociatedsequence)) {
                    associatedsequenceListNewAssociatedsequence.getOccurrenceelementList().add(occurrenceelement);
                    associatedsequenceListNewAssociatedsequence = em.merge(associatedsequenceListNewAssociatedsequence);
                }
            }
            for (Preparation preparationListOldPreparation : preparationListOld) {
                if (!preparationListNew.contains(preparationListOldPreparation)) {
                    preparationListOldPreparation.getOccurrenceelementList().remove(occurrenceelement);
                    preparationListOldPreparation = em.merge(preparationListOldPreparation);
                }
            }
            for (Preparation preparationListNewPreparation : preparationListNew) {
                if (!preparationListOld.contains(preparationListNewPreparation)) {
                    preparationListNewPreparation.getOccurrenceelementList().add(occurrenceelement);
                    preparationListNewPreparation = em.merge(preparationListNewPreparation);
                }
            }
            for (Specimen specimenListOldSpecimen : specimenListOld) {
                if (!specimenListNew.contains(specimenListOldSpecimen)) {
                    specimenListOldSpecimen.setOccurrenceelement(null);
                    specimenListOldSpecimen = em.merge(specimenListOldSpecimen);
                }
            }
            for (Specimen specimenListNewSpecimen : specimenListNew) {
                if (!specimenListOld.contains(specimenListNewSpecimen)) {
                    Occurrenceelement oldOccurrenceelementOfSpecimenListNewSpecimen = specimenListNewSpecimen.getOccurrenceelement();
                    specimenListNewSpecimen.setOccurrenceelement(occurrenceelement);
                    specimenListNewSpecimen = em.merge(specimenListNewSpecimen);
                    if (oldOccurrenceelementOfSpecimenListNewSpecimen != null && !oldOccurrenceelementOfSpecimenListNewSpecimen.equals(occurrenceelement)) {
                        oldOccurrenceelementOfSpecimenListNewSpecimen.getSpecimenList().remove(specimenListNewSpecimen);
                        oldOccurrenceelementOfSpecimenListNewSpecimen = em.merge(oldOccurrenceelementOfSpecimenListNewSpecimen);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = occurrenceelement.getIdoccurrenceelement();
                if (findOccurrenceelement(id) == null) {
                    throw new NonexistentEntityException("The occurrenceelement with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Occurrenceelement occurrenceelement;
            try {
                occurrenceelement = em.getReference(Occurrenceelement.class, id);
                occurrenceelement.getIdoccurrenceelement();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The occurrenceelement with id " + id + " no longer exists.", enfe);
            }
            Sex sex = occurrenceelement.getSex();
            if (sex != null) {
                sex.getOccurrenceelementList().remove(occurrenceelement);
                sex = em.merge(sex);
            }
            Reproductivecondition reproductivecondition = occurrenceelement.getReproductivecondition();
            if (reproductivecondition != null) {
                reproductivecondition.getOccurrenceelementList().remove(occurrenceelement);
                reproductivecondition = em.merge(reproductivecondition);
            }
            Lifestage lifestage = occurrenceelement.getLifestage();
            if (lifestage != null) {
                lifestage.getOccurrenceelementList().remove(occurrenceelement);
                lifestage = em.merge(lifestage);
            }
            Establishmentmean establishmentmean = occurrenceelement.getEstablishmentmean();
            if (establishmentmean != null) {
                establishmentmean.getOccurrenceelementList().remove(occurrenceelement);
                establishmentmean = em.merge(establishmentmean);
            }
            Disposition disposition = occurrenceelement.getDisposition();
            if (disposition != null) {
                disposition.getOccurrenceelementList().remove(occurrenceelement);
                disposition = em.merge(disposition);
            }
            Behavior behavior = occurrenceelement.getBehavior();
            if (behavior != null) {
                behavior.getOccurrenceelementList().remove(occurrenceelement);
                behavior = em.merge(behavior);
            }
            List<Individual> individualList = occurrenceelement.getIndividualList();
            for (Individual individualListIndividual : individualList) {
                individualListIndividual.getOccurrenceelementList().remove(occurrenceelement);
                individualListIndividual = em.merge(individualListIndividual);
            }
            List<Recordedby> recordedbyList = occurrenceelement.getRecordedbyList();
            for (Recordedby recordedbyListRecordedby : recordedbyList) {
                recordedbyListRecordedby.getOccurrenceelementList().remove(occurrenceelement);
                recordedbyListRecordedby = em.merge(recordedbyListRecordedby);
            }
            List<Associatedsequence> associatedsequenceList = occurrenceelement.getAssociatedsequenceList();
            for (Associatedsequence associatedsequenceListAssociatedsequence : associatedsequenceList) {
                associatedsequenceListAssociatedsequence.getOccurrenceelementList().remove(occurrenceelement);
                associatedsequenceListAssociatedsequence = em.merge(associatedsequenceListAssociatedsequence);
            }
            List<Preparation> preparationList = occurrenceelement.getPreparationList();
            for (Preparation preparationListPreparation : preparationList) {
                preparationListPreparation.getOccurrenceelementList().remove(occurrenceelement);
                preparationListPreparation = em.merge(preparationListPreparation);
            }
            List<Specimen> specimenList = occurrenceelement.getSpecimenList();
            for (Specimen specimenListSpecimen : specimenList) {
                specimenListSpecimen.setOccurrenceelement(null);
                specimenListSpecimen = em.merge(specimenListSpecimen);
            }
            em.remove(occurrenceelement);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Occurrenceelement> findOccurrenceelementEntities() {
        return findOccurrenceelementEntities(true, -1, -1);
    }

    public List<Occurrenceelement> findOccurrenceelementEntities(int maxResults, int firstResult) {
        return findOccurrenceelementEntities(false, maxResults, firstResult);
    }

    private List<Occurrenceelement> findOccurrenceelementEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Occurrenceelement.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Occurrenceelement findOccurrenceelement(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Occurrenceelement.class, id);
        } finally {
            em.close();
        }
    }

    public int getOccurrenceelementCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Occurrenceelement> rt = cq.from(Occurrenceelement.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
